package com.lengxf;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class CompleteFutureDemo {
    public static void main(String[] args) throws Exception {
         strFuture()
                .thenApplyAsync(future -> future + " : 1")
                .thenApply(e -> {
                    if (e.contains("1")) {
                        throw new RuntimeException("yichangle");
                    }
                    return e;
                })
                .handle((str, throwable) -> {
                    if (throwable != null) {
                        return throwable.getMessage();
                    } else {
                        return str;
                    }
                }).whenComplete( (e,a) -> System.out.println(e));
    }

    private static CompletableFuture<String> strFuture() {
        return CompletableFuture.completedFuture(str());
    }


    private static String str() {
        String s = "hello";
        System.out.println("str : " + s);
        return s;
    }

}